package com.example.human_resource_management.controller;


import com.example.human_resource_management.common.ResponseData;
import com.example.human_resource_management.config.UserContextUtils;
import com.example.human_resource_management.entry.po.Employee;
import com.example.human_resource_management.entry.po.EmployeeForum;
import com.example.human_resource_management.entry.po.Training;
import com.example.human_resource_management.service.EmployeeForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/forum")
public class EmployeeForumController {
    @Autowired
    private EmployeeForumService employeeForumService;



    // 员工查询论坛所有内容
    @GetMapping("lt")
    public ResponseData<List<EmployeeForum>> getEmployeeForumList() {
        List<EmployeeForum> employeeForums = employeeForumService.selectEmployeeForumList();
        return ResponseData.ok(employeeForums);
    }

    //员工查询自己发布的内容
/*
    @GetMapping("ltz")
    public ResponseData<EmployeeForum> getEmployeeForum() {
        Long empId = UserContextUtils.getEmpId();
        if (empId != null) {
            EmployeeForum employeeForum = employeeForumService.getEmployeeForumById(empId);
            if (employeeForum != null) {
                return ResponseData.ok(employeeForum);
            }
        }
        return ResponseData.warn("未获取到员工信息");
    }
*/
    //员工查询自己发布的内容
    @GetMapping("lll")
    public ResponseData<List<EmployeeForum>> getEmployeeForums() {
        Long empId = UserContextUtils.getEmpId();
        if (empId == null) {
            return ResponseData.warn("未获取到员工信息");
        }

        List<EmployeeForum> forums = employeeForumService.getEmployeeForumsByEmpId(empId);
        if (forums.isEmpty()) {
            return ResponseData.warn("未找到该员工发布的帖子");
        }

        return ResponseData.ok(forums);
    }

    //员工新增帖子
    @PostMapping("create")
    public ResponseData<String> createEmployeeForm(@RequestBody EmployeeForum employeeForum) {
        Long empId = UserContextUtils.getEmpId();
        if (empId == null) {
            return ResponseData.warn("未获取到员工信息");
        }
        EmployeeForum employeeForum1 = new EmployeeForum();
        employeeForum1.setEmpId(empId);
        employeeForum1.setTitle(employeeForum.getTitle());
        employeeForum1.setContent(employeeForum.getContent());
        return employeeForumService.createEmployeeForum(employeeForum1);
    }

    //员工删除自己帖子
    @PostMapping("delete")
    public ResponseData<String> deleteEmployeeForum(@RequestBody  Map<String, Long> request) {
        Long empId = UserContextUtils.getEmpId();
        long postId = request.get("postId");
        if (empId == null) {
            return ResponseData.warn("未获取到员工信息");
        }
        return employeeForumService.deleteEmployeeForum(empId, postId);
    }











}
